package com.c.Servlet;

import com.alibaba.fastjson.JSON;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet(name = "BaseSServlet", value = "/BaseServlet")
public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码格式
        request.setCharacterEncoding("UTF-8");
        //设置响应编码
        response.setContentType("text/html;charset=UTF-8");
        //获取执行方法名字
        String m = request.getParameter("m");
        if (m != null && !"".equals(m)) {
            try {
                //通过反射获取对应方法
                Method method = this.getClass()
                        .getDeclaredMethod(m,
                                HttpServletRequest.class,
                                HttpServletResponse.class);
                //执行方法
                Object value = method.invoke(this, request, response);
                //将结果响应
                response.getWriter().write(JSON.toJSONString(value));
            } catch (Exception e) {
                response.getWriter().write("找不到对应方法!!!");
                e.printStackTrace();
            }
        }else {
            response.getWriter().write("找不到对应方法!!!");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
